package com.suanfa.collect;

/**
 * 二维举证中查找目标值
 */
public class BinMatrix {

    public static void main(String[] args) {
        int[][] arr = new int[][]{{1,3,5,7},{10,11,16,20},{23,30,34,60}};
        int target = 3;
        System.out.println(getIndex(arr, target));
    }


    public static boolean getIndex(int[][] arr, int target){
        if(arr.length ==0){
            return  false;
        }
        for(int i =0; i< arr.length; i++){
            int[] nums = arr[i];
            int max = nums[nums.length-1];
            if(target == max){
                return true;
            }
            if(target < max){
                return  getElement(nums, target);
            }
        }
        return false;
    }

    public static boolean getElement(int[] arr, int target){
        int s = 0, e = arr.length;

        while(s < e){
            int mid = (s+e) >>1;
            if(arr[mid] == target){
                return true;
            }
            if(arr[mid] < target){
                s = mid + 1;
            }else {
                e = mid;
            }

        }
        return false;
    }
}
